//
//  RegistViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "RegistViewController.h"
#import "SetupPasswordViewController.h"
#import "ServiceProtocolViewController.h"

@interface RegistViewController ()
{
    NSString *_codeType;
    dispatch_queue_t queue ;
    dispatch_source_t _timer;
}

@property (nonatomic, assign) BOOL isAgreed;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = self.forgetPassword ? @"找回登录密码" : @"注册";
    [self setupCustomBackItem];
    
    self.isAgreed = YES;
    
    if (self.forgetPassword) {
        
        self.agreeB.hidden = YES;
        self.agreeL.hidden = YES;
        self.serviceProtocolB.hidden = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)setIsAgreed:(BOOL)isAgreed
{
    _isAgreed = isAgreed;
    
    if (_isAgreed) {
        [self.agreeB setImage:[UIImage imageNamed:@"regist_agree"] forState:0];
    } else {
        [self.agreeB setImage:[UIImage imageNamed:@"regist_disagree"] forState:0];
    }
}

- (IBAction)button_getValidateCode:(id)sender {
    
    if (self.accountTF.text.length < 11) {
        [self.view makeToast:@"请输入正确的手机号" duration:2.0f position:@"center"];
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    if (self.forgetPassword) {
        _codeType = @"02";
    } else {
        _codeType = @"01";
    }
    [TDHttpEngine requestForGetMsgCodeWithCustMobile:self.accountTF.text codeType:_codeType complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            [self timerFireMethod];
            //NSLog(@"%@", msg);
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (IBAction)button_agreeService:(id)sender {
    
    self.isAgreed = !self.isAgreed;
}

- (IBAction)button_serviceProtocol:(id)sender {
    
    ServiceProtocolViewController *controller = [[ServiceProtocolViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)button_nextStep:(id)sender {
    
    if (self.validateCodeTF.text.length == 0) {
        [self.view makeToast:@"请输入收到的验证码" duration:2.0f position:@"center"];
        return;
    }
    
    if (self.accountTF.text.length < 11) {
        [self.view makeToast:@"请输入正确的手机号" duration:2.0f position:@"center"];
        return;
    }
    
    if (!self.isAgreed) {
        [self.view makeToast:@"请勾选同意服务协议选项" duration:2.0f position:@"center"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    if (self.forgetPassword) {
        _codeType = @"02";
    } else {
        _codeType = @"01";
    }
    
    [TDHttpEngine requestForVerifyMsgCodeWithCustMobile:self.accountTF.text codeType:_codeType msgCode:self.validateCodeTF.text isForgetPwd:self.forgetPassword complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            
            SetupPasswordViewController *controller = [[SetupPasswordViewController alloc] init];
            controller.forgetPassword = self.forgetPassword;
            controller.account = self.accountTF.text;
            controller.validateCode = self.validateCodeTF.text;
            [self.navigationController pushViewController:controller animated:YES];
        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

//验证码跳秒方法
- (void)timerFireMethod
{
    self.getValidateCodeB.userInteractionEnabled = NO;
    __block int timeout = 59; //倒计时时间
    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.getValidateCodeB.userInteractionEnabled = YES;
                [self.getValidateCodeB setTitle:@"重新获取验证码" forState:0];
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d秒", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.getValidateCodeB setTitle:[NSString stringWithFormat:@"%@",strTime] forState:0];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

@end
